package com.et114.components.rule.support;

import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.compiler.PackageBuilder;
import org.springframework.core.io.Resource;

import java.io.InputStreamReader;
import java.io.Reader;

/**
 * 
 */
public class DRLRuleBaseLoader extends AbstractFileRuleBaseLoader {
	
	/**
	 * @see org.springside.modules.rule.RuleBaseLoader#buildRuleBase()
	 */
	public RuleBase buildRuleBase ( ) throws Exception {
		RuleBase ruleBase = RuleBaseFactory.newRuleBase ( );
		Resource [ ] resources = getRuleFiles ( ruleFiles );
		for ( int i = 0 ; i < resources.length ; i ++ ) {
			Resource resource = resources [ i ];
			PackageBuilder builder = new PackageBuilder ( );
			
			Reader source = new InputStreamReader (
					resource.getInputStream ( ) , "UTF-8" );
			builder.addPackageFromDrl ( source );
			ruleBase.addPackage ( builder.getPackage ( ) );
		}
		
		return ruleBase;
	}
}
